/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.client.gui;

import java.awt.Component;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import sshtunneler.client.ssh.SSHConnection;
import sshtunneler.client.ssh.SSHConnectionContainer;
import sshtunneler.library.Server;
import sshtunneler.library.Tunnel;
import sshtunneler.server_tunnel.tree.ServerNode;
import sshtunneler.server_tunnel.tree.TunnelNode;

public class ServerTunnelCellRenderer extends DefaultTreeCellRenderer {
	private static final long serialVersionUID = 5193019875471850018L;
	protected Icon icon_server_disconnected;
	protected Icon icon_server_connected;
	protected Icon icon_tunnel_disconnected;
	protected Icon icon_tunnel_connected;

	public ServerTunnelCellRenderer() {
		try {
			ImageIcon disconnected = new ImageIcon(ImageIO.read(this.getClass()
					.getClassLoader().getResourceAsStream(
							"tunnel_disconnected.png")));
			ImageIcon connected = new ImageIcon(ImageIO.read(this.getClass()
					.getClassLoader().getResourceAsStream(
							"tunnel_connected.png")));
			this.icon_server_disconnected = disconnected;
			this.icon_server_connected = connected;
			this.icon_tunnel_disconnected = disconnected;
			this.icon_tunnel_connected = connected;
		} catch (IOException ex) {
			Logger.getLogger(ServerTunnelCellRenderer.class.getName()).log(
					Level.SEVERE, null, ex);
		}
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {

		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
				row, hasFocus);

		Object selectedCell = value;
		if (selectedCell instanceof ServerNode) {
			ServerNode node = (ServerNode) selectedCell;
			Server server = node.getServer();

			if (SSHConnectionContainer.inst.hasConnectionTo(server)) {
				this.setIcon(this.icon_server_connected);
			} else {
				this.setIcon(this.icon_server_disconnected);
			}
		} else if (selectedCell instanceof TunnelNode) {
			TunnelNode node = (TunnelNode) selectedCell;
			Server server = node.getServer();
			Tunnel tunnel = node.getTunnel();
			SSHConnection conn = SSHConnectionContainer.inst
					.getConnectionTo(server);

			if (conn != null && conn.tunnel_is_established(tunnel)) {
				this.setIcon(this.icon_tunnel_connected);
			} else {
				this.setIcon(this.icon_tunnel_disconnected);
			}
		}

		return this;
	}
}
